package com.nwpu.coursesupport.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.criteria.ClassRespositorySearchCriteria;
import com.nwpu.coursesupport.criteria.RespositorySearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.service.imple.RespositoryService;
import com.nwpu.coursesupport.tool.JSONUtils;

@Controller
@RequestMapping("/respository")
public class RespositoryController extends AbstractController {

	@Autowired
	private RespositoryService respositoryService;
	
	@RequestMapping("/listByClassId")
	public String listByClassId(@RequestParam("classId")int classId,Integer pageNo,HttpServletRequest request){
		JSONResult rst=new JSONResult();
		rst.fail();
		ClassRespositorySearchCriteria sc=new ClassRespositorySearchCriteria();
		sc.setClassId(classId);
		sc.setPageNo(pageNo==null?1:pageNo);
		try{
			rst = this.respositoryService.listByClassId(sc);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			logger.error(e.getMessage());
			rst.fail("出现错误:"+e.getMessage());
		}
		return "/stupage/classResource";
	}
	
	@RequestMapping("/ajaxListByClassId")
	@ResponseBody
	public JSONResult ajaxListByClassId(String data){
		JSONResult rst=new JSONResult();
		rst.fail();
		ClassRespositorySearchCriteria sc=JSONUtils.json2pojo(data, ClassRespositorySearchCriteria.class);
		if(sc==null){
			return rst.fail("参数转换错误");
		}
		try{
			rst = this.respositoryService.listByClassId(sc);
		}catch(Exception e){
			logger.error(e.getMessage());
			rst.fail("出现错误:"+e.getMessage());
		}
		return rst;
	}
	
	@RequestMapping("/listData")
	public String list(@RequestParam("courseId")int courseId,Integer pageNo,HttpServletRequest request) {
		JSONResult rst=new JSONResult();
		rst.fail();
		RespositorySearchCriteria sc=new RespositorySearchCriteria();
		sc.setCourseId(courseId);
		pageNo=pageNo==null?1:pageNo;
		sc.setPageNo(pageNo);
		try{
			rst = this.respositoryService.list(sc);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			logger.error(e.getMessage());
			rst.fail("出现错误:"+e.getMessage());
		}
		return "/teapage/courseResourceIndex";
	}

	@RequestMapping("/ajaxListData")
	@ResponseBody
	public JSONResult ajaxListData(@RequestParam("data") String data) {
		JSONResult result = invokeService("respository", "list", data);
		return result; 
	}

	@RequestMapping("/ajaxBatchCreate")
	@ResponseBody
	public JSONResult ajaxBatchCreate(@RequestParam("data") String data) {
		JSONResult result = invokeService("respository", "batchCreate", data);
		return result;
	}
	
	@RequestMapping("/ajaxCreate")
	@ResponseBody
	public JSONResult ajaxCreate(@RequestParam("data") String data) {
		JSONResult result = invokeService("respository", "create", data);
		return result;
	}

	@RequestMapping("/ajaxDelete")
	@ResponseBody
	public JSONResult ajaxDelete(@RequestParam("id") int id) {
		JSONResult result = invokeService("respository", "remove", "{\"id\":\"" + id + "\"}");
		return result;
	}

	@RequestMapping("/ajaxDeleteAll")
	@ResponseBody
	public JSONResult ajaxDeleteAll(@RequestParam("data") String data) {
		JSONResult result = invokeService("respository", "removeAll", data);
		return result;
	}

}
